home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr49 / fgfx12.zip / EFFECTS.C < prev    next >
Text File  |  1995-02-14  |  23KB  |  672 lines

  1. /****************************************************************************\
  2. *                                                                            *
  3. *  EFFECTS.C                                                                 *
  4. *                                                                            *
  5. *  This program demonstrates several methods of fading in an image from an   *
  6. *  off-screen video page using either Fastgraph or Fastgraph/Light.  The set *
  7. *  of routines provided herein are written for 320x200 graphics video modes, *
  8. *  but they could easily be extended to work in other resolutions.           *
  9. *                                                                            *
  10. *  The examples are by no means all inclusive.  Rather, their purpose is to  *
  11. *  illustrate a few methods of creating special effects with Fastgraph or    *
  12. *  Fastgraph/Light.                                                          *
  13. *                                                                            *
  14. *  To compile this program and link it with Fastgraph version 4.0:           *
  15. *                                                                            *
  16. *     Borland C++:                                                           *
  17. *        BCC -ms EFFECTS.C FGS.LIB                                           *
  18. *                                                                            *
  19. *     Microsoft C/C++ and Visual C++:                                        *
  20. *        CL /AS EFFECTS.C /link FGS                                          *
  21. *                                                                            *
  22. *     Microsoft QuickC:                                                      *
  23. *        QCL /AS EFFECTS.C /link FGS                                         *
  24. *                                                                            *
  25. *     Microsoft Visual C++ 32-bit Edition with Phar Lap TNT extender:        *
  26. *        CL EFFECTS.C /link /stub:\TNT\BIN\GOTNT.EXE FG32VC.LIB              *
  27. *                                                                            *
  28. *     Power C:                                                               *
  29. *        PC /ms EFFECTS                                                      *
  30. *        PCL EFFECTS ;FGS                                                    *
  31. *                                                                            *
  32. *     Turbo C and Turbo C++:                                                 *
  33. *        TCC -ms EFFECTS.C FGS.LIB                                           *
  34. *                                                                            *
  35. *     Watcom C/C++ (16 bits):                                                *
  36. *        WCL /ms EFFECTS.C FGS.LIB                                           *
  37. *                                                                            *
  38. *     Watcom C/C++ (32 bits) with Rational Systems DOS/4GW extender:         *
  39. *        WCL386 /l=dos4g EFFECTS.C FG32.LIB FG32DPMI.LIB                     *
  40. *                                                                            *
  41. *     Zortech C++:                                                           *
  42. *        ZTC -ms EFFECTS.C FGS.LIB                                           *
  43. *                                                                            *
  44. *  This program also can be linked with Fastgraph/Light 4.0 if you replace   *
  45. *  the FGS library references with FGLS.                                     *
  46. *                                                                            *
  47. *  Fastgraph (tm) and Fastgraph/Light (tm) are graphics libraries published  *
  48. *  by Ted Gruber Software.  For more info, please call, write, or FAX.       *
  49. *                                                                            *
  50. *  Ted Gruber Software                           orders/info (702) 735-1980  *
  51. *  PO Box 13408                                          FAX (702) 735-4603  *
  52. *  Las Vegas, NV  89112                                  BBS (702) 796-7134  *
  53. *                                                                            *
  54. \****************************************************************************/
  55.  
  56. #include <fastgraf.h>
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60.  
  61. /* function prototypes */
  62.  
  63. void main(void);
  64.  
  65. void announce(char *);
  66. int  irandom(int,int);
  67.  
  68. void curtain(int);
  69. void diagonal_fade(int);
  70. void horizontal_random_fade(int);
  71. void inward_tunnel_effect(int);
  72. void outward_tunnel_effect(int);
  73. void spiral_dual(int);
  74. void spiral_layered(int);
  75. void spiral_normal(int);
  76. void split_screen(int);
  77. void unveil(int);
  78. void venetian_blind(int);
  79.  
  80. /* global variables */
  81.  
  82. int delay;
  83. int scroll_delay;
  84.  
  85. /* main program */
  86.  
  87. void main()
  88. {
  89.    int old_mode, new_mode;
  90.    unsigned int count;
  91.    unsigned long start_time;
  92.  
  93.    /* in case we're compiling for protected mode */
  94.  
  95.    fg_initpm();
  96.  
  97.    /* make sure a 320x200 color graphics mode is available */
  98.  
  99.    new_mode = fg_bestmode(320,200,2);
  100.    if (new_mode < 0 || new_mode == 12)
  101.    {
  102.       printf("This program requires a 320x200 color graphics mode.\n");
  103.       exit(1);
  104.    }
  105.  
  106.    /* determine the number of delay units per half clock tick */
  107.  
  108.    delay = fg_measure() / 2;
  109.  
  110.    /* initialize Fastgraph for the selected video mode */
  111.  
  112.    old_mode = fg_getmode();
  113.    fg_setmode(new_mode);
  114.    fg_allocate(1);
  115.  
  116.    /* display a packed pixel run file on a hidden page */
  117.  
  118.    fg_sethpage(1);
  119.    fg_setpage(1);
  120.    fg_move(0,199);
  121.    fg_showppr("FG.PPR",320);
  122.    fg_setpage(0);
  123.  
  124.    /* compute the number of delay units needed to make the text scroll */
  125.    /* down at the same rate, regardless of the CPU speed or video mode */
  126.  
  127.    count = 0;
  128.    fg_waitfor(1);
  129.    start_time = fg_getclock();
  130.    do
  131.    {
  132.       fg_scroll(0,319,0,7,4,1);
  133.       count++;
  134.    }
  135.    while (fg_getclock() == start_time);
  136.  
  137.    scroll_delay = (delay / 8) - (delay * 2) / count;
  138.    if (scroll_delay < 0) scroll_delay = 0;
  139.  
  140.    /* demonstrate the inward tunnel effect */
  141.  
  142.    announce("inward tunnel effect");
  143.    inward_tunnel_effect(0);
  144.    fg_waitfor(27);
  145.    announce("inward tunnel effect with delay");
  146.    inward_tunnel_effect(delay);
  147.    fg_waitfor(27);
  148.  
  149.    /* demonstrate the outward tunnel effect */
  150.  
  151.    announce("outward tunnel effect");
  152.    outward_tunnel_effect(0);
  153.    fg_waitfor(27);
  154.    announce("outward tunnel effect with delay");
  155.    outward_tunnel_effect(delay);
  156.    fg_waitfor(27);
  157.  
  158.    /* demonstrate the diagonal fade */
  159.  
  160.    announce("diagonal fade");
  161.    diagonal_fade(0);
  162.    fg_waitfor(27);
  163.    announce("diagonal fade with delay");
  164.    diagonal_fade(delay/2);
  165.    fg_waitfor(27);
  166.  
  167.    /* demonstrate the horizontal random fade */
  168.  
  169.    announce("horizontal random fade");
  170.    horizontal_random_fade(delay);
  171.    fg_waitfor(27);
  172.  
  173.    /* demonstrate the curtain effect */
  174.  
  175.    announce("curtain");
  176.    curtain(delay/8);
  177.    fg_waitfor(27);
  178.  
  179.    /* demonstrate the spiral effect */
  180.  
  181.    announce("spiral");
  182.    spiral_normal(delay*2);
  183.    fg_waitfor(27);
  184.  
  185.    /* demonstrate the layered spiral effect */
  186.  
  187.    announce("layered spiral");
  188.    spiral_layered(delay);
  189.    fg_waitfor(27);
  190.  
  191.    /* demonstrate the dual spiral effect */
  192.  
  193.    announce("dual spiral");
  194.    spiral_dual(delay/2);
  195.    fg_waitfor(27);
  196.  
  197.    /* demonstrate the split screen effect */
  198.  
  199.    announce("split screen");
  200.    split_screen(delay/2);
  201.    fg_waitfor(27);
  202.  
  203.    /* demonstrate the unveil effect */
  204.  
  205.    announce("unveil");
  206.    unveil(delay/4);
  207.    fg_waitfor(27);
  208.  
  209.    /* demonstrate the "venetian blind" effect */
  210.  
  211.    announce("venetian blind");
  212.    venetian_blind(delay);
  213.    fg_waitfor(27);
  214.  
  215.    /* restore the original video mode and screen attributes */
  216.  
  217.    fg_freepage(1);
  218.    fg_setmode(old_mode);
  219.    fg_reset();
  220. }
  221.  
  222. /****************************************************************************\
  223. *                                                                            *
  224. *  announce                                                                  *
  225. *                                                                            *
  226. *  Display the name of the special effect we're about to see.                *
  227. *                                                                            *
  228. \****************************************************************************/
  229.  
  230. void announce(message)
  231. char *message;
  232. {
  233.    register int y;
  234.  
  235.    /* clear the screen */
  236.  
  237.    fg_erase();
  238.  
  239.    /* display the specified message at the top row */
  240.  
  241.    fg_setcolor(10);
  242.    fg_justify(0,-1);
  243.    fg_move(160,15);
  244.    fg_fontsize(16);
  245.    fg_print(message,strlen(message));
  246.  
  247.    /* scroll the message to the center of the screen */
  248.  
  249.    fg_setcolor(0);
  250.  
  251.    for (y = 0; y < 96; y+=4)
  252.    {
  253.       fg_scroll(0,319,y,y+15,4,1);
  254.       fg_stall(scroll_delay);
  255.    }
  256.  
  257.    /* wait 1.5 seconds */
  258.  
  259.    fg_waitfor(27);
  260. }
  261.  
  262. /****************************************************************************\
  263. *                                                                            *
  264. *  irandom                                                                   *
  265. *                                                                            *
  266. *  Random number generator used in some of the effects.  It returns an       *
  267. *  integer between min and max inclusive.                                    *
  268. *                                                                            *
  269. \****************************************************************************/
  270.  
  271. int irandom(min,max)
  272. int min, max;
  273. {
  274.    return(rand() % (max-min+1) + min);
  275. }
  276.  
  277. /****************************************************************************\
  278. *                                                                            *
  279. *  curtain                                                                   *
  280. *                                                                            *
  281. *  Reveal each row, one at a time, starting from the bottom and proceeding   *
  282. *  to the top.  This gives the effect of a curtain rising, hence the name.   *
  283. *                                                                            *
  284. \****************************************************************************/
  285.  
  286. void curtain(delay)
  287. int delay;
  288. {
  289.    register int y;
  290.  
  291.    for (y = 199; y >= 0; y--)
  292.    {
  293.       fg_restore(0,319,y,y);
  294.       fg_stall(delay);
  295.    }
  296. }
  297.  
  298. /****************************************************************************\
  299. *                                                                            *
  300. *  diagonal_fade                                                             *
  301. *                                                                            *
  302. *  This reveals the hidden page in two diagonal segments, separated by an    *
  303. *  imaginary line extending from the lower left corner to the upper right    *
  304. *  corner of the screen.  We start with the top line of the left segment and *
  305. *  the bottom line of the right segment, and continue until the entire       *
  306. *  screen is revealed.                                                       *
  307. *                                                                            *
  308. \****************************************************************************/
  309.  
  310. void diagonal_fade(delay)
  311. int delay;
  312. {
  313.    int xmin, xmax;
  314.    int ymin, ymax;
  315.  
  316.    xmin = 0;
  317.    xmax = 319;
  318.    ymin = 0;
  319.    ymax = 199;
  320.  
  321.    while (xmax > 0)
  322.    {
  323.       fg_restore(0,xmax,ymin,ymin+4);
  324.       fg_restore(xmin,319,ymax-4,ymax);
  325.       fg_stall(delay);
  326.  
  327.       xmin += 8;
  328.       xmax -= 8;
  329.       ymin += 5;
  330.       ymax -= 5;
  331.    }
  332. }
  333.  
  334. /****************************************************************************\
  335. *                                                                            *
  336. *  horizontal_random_fade                                                    *
  337. *                                                                            *
  338. *  In this effect, the screen is divided into a series of two-pixel high     *
  339. *  rows.  Each row is revealed in random parts from left to right.  This     *
  340. *  process repeats 20 times, once for each row.  At the end, a call to the   *
  341. *  fg_restore routine guarantees that all rows are transferred.              *
  342. *                                                                            *
  343. \****************************************************************************/
  344.  
  345. void horizontal_random_fade(delay)
  346. int delay;
  347. {
  348.    register int i, j;
  349.    int xmin, xmax;
  350.    int y;
  351.    int xpos[100];
  352.  
  353.    for (j = 0; j < 100; j++)
  354.       xpos[j] = 0;
  355.  
  356.    for (i = 0; i < 20; i++)
  357.    {
  358.       for (j = 0; j < 100; j++)
  359.       {
  360.          xmin = xpos[j];
  361.          if (xmin < 320)
  362.          {
  363.             xmax = xmin + irandom(1,10) * 8;
  364.             if (xmax > 320) xmax = 320;
  365.             y = j * 2;
  366.             fg_restore(xmin,xmax-1,y,y+1);
  367.             xpos[j] = xmax;
  368.          }
  369.       }
  370.       fg_stall(delay);
  371.    }
  372.  
  373.    /* make sure we got them all */
  374.  
  375.    fg_restore(0,319,0,199);
  376. }
  377.  
  378. /****************************************************************************\
  379. *                                                                            *
  380. *  inward_tunnel_effect                                                      *
  381. *                                                                            *
  382. *  Starting at the screen edges, reveal the screen through a series of       *
  383. *  concentric hollow rectangles.                                             *
  384. *                                                                            *
  385. \****************************************************************************/
  386.  
  387. void inward_tunnel_effect(delay)
  388. int delay;
  389. {
  390.    int xmin, xmax;
  391.    int ymin, ymax;
  392.  
  393.    xmin = 0;
  394.    xmax = 319;
  395.    ymin = 0;
  396.    ymax = 199;
  397.  
  398.    while (xmin < xmax)
  399.    {
  400.       fg_restore(0,319,ymin,ymin+4);
  401.       fg_restore(xmax-7,xmax,0,199);
  402.       fg_restore(0,319,ymax-4,ymax);
  403.       fg_restore(xmin,xmin+7,0,199);
  404.       fg_stall(delay);
  405.  
  406.       xmin += 8;
  407.       xmax -= 8;
  408.       ymin += 5;
  409.       ymax -= 5;
  410.    }
  411. }
  412.  
  413. /****************************************************************************\
  414. *                                                                            *
  415. *  outward_tunnel_effect                                                     *
  416. *                                                                            *
  417. *  Starting at the screen center, reveal the screen through a series of      *
  418. *  concentric hollow rectangles.                                             *
  419. *                                                                            *
  420. \****************************************************************************/
  421.  
  422. void outward_tunnel_effect(delay)
  423. int delay;
  424. {
  425.    int xmin, xmax;
  426.    int ymin, ymax;
  427.  
  428.    xmin = 152;
  429.    xmax = 167;
  430.    ymin = 95;
  431.    ymax = 104;
  432.  
  433.    while (xmin >= 0)
  434.    {
  435.       fg_restore(xmin,xmax,ymin,ymin+5);
  436.       fg_restore(xmax-7,xmax,ymin,ymax);
  437.       fg_restore(xmin,xmax,ymax-4,ymax);
  438.       fg_restore(xmin,xmin+7,ymin,ymax);
  439.       fg_stall(delay);
  440.  
  441.       xmin -= 8;
  442.       xmax += 8;
  443.       ymin -= 5;
  444.       ymax += 5;
  445.    }
  446. }
  447.  
  448. /****************************************************************************\
  449. *                                                                            *
  450. *  spiral_dual                                                               *
  451. *                                                                            *
  452. *  In this effect, we reveal the screen through two spirals.  One spiral     *
  453. *  emanates clockwise from the screen edges to the screen center, while the  *
  454. *  other emanates counterclockwise from the center to the screen edges.      *
  455. *                                                                            *
  456. \****************************************************************************/
  457.  
  458. void spiral_dual(delay)
  459. int delay;
  460. {
  461.    int xmin_outer, xmax_outer;
  462.    int ymin_outer, ymax_outer;
  463.    int xmin_inner, xmax_inner;
  464.    int ymin_inner, ymax_inner;
  465.  
  466.    xmin_outer = 0;
  467.    xmax_outer = 319;
  468.    ymin_outer = 0;
  469.    ymax_outer = 199;
  470.  
  471.    xmin_inner = 152;
  472.    xmax_inner = 167;
  473.    ymin_inner = 95;
  474.    ymax_inner = 104;
  475.  
  476.    while (xmin_outer < xmin_inner)
  477.    {
  478.       fg_restore(xmin_outer,xmax_outer,ymin_outer,ymin_outer+4);
  479.       fg_stall(delay);
  480.       fg_restore(xmin_inner,xmax_inner,ymax_inner-4,ymax_inner);
  481.       fg_stall(delay);
  482.       fg_restore(xmax_outer-7,xmax_outer,ymin_outer,ymax_outer);
  483.       fg_stall(delay);
  484.       fg_restore(xmax_inner+1,xmax_inner+8,ymin_inner,ymax_inner);
  485.       fg_stall(delay);
  486.       fg_restore(xmin_outer,xmax_outer,ymax_outer-4,ymax_outer);
  487.       fg_stall(delay);
  488.       fg_restore(xmin_inner-8,xmax_inner,ymin_inner,ymin_inner+4);
  489.       fg_stall(delay);
  490.       fg_restore(xmin_outer,xmin_outer+7,ymin_outer,ymax_outer);
  491.       fg_stall(delay);
  492.       fg_restore(xmin_inner-8,xmin_inner-1,ymin_inner,ymax_inner+5);
  493.       fg_stall(delay);
  494.  
  495.       xmin_outer += 8;
  496.       xmax_outer -= 8;
  497.       ymin_outer += 5;
  498.       ymax_outer -= 5;
  499.  
  500.       xmin_inner -= 8;
  501.       xmax_inner += 8;
  502.       ymin_inner -= 5;
  503.       ymax_inner += 5;
  504.    }
  505. }
  506.  
  507. /****************************************************************************\
  508. *                                                                            *
  509. *  spiral_layered                                                            *
  510. *                                                                            *
  511. *  This effect is similar to the normal spiral.  Instead of revealing the    *
  512. *  screen in one iteration, this effect does so in four iterations (layers), *
  513. *  each moving more toward the screen center.                                *
  514. *                                                                            *
  515. \****************************************************************************/
  516.  
  517. void spiral_layered(delay)
  518. int delay;
  519. {
  520.    register int i;
  521.    int xmin, xmax;
  522.    int ymin, ymax;
  523.  
  524.    for (i = 0; i < 4; i++)
  525.    {
  526.       xmin = i * 8;
  527.       xmax = 319 - xmin;
  528.       ymin = i * 5;
  529.       ymax = 199 - ymin;
  530.  
  531.       while (xmin < xmax)
  532.       {
  533.          fg_restore(xmin,xmax,ymin,ymin+4);
  534.          fg_stall(delay);
  535.          fg_restore(xmax-7,xmax,ymin,ymax);
  536.          fg_stall(delay);
  537.          fg_restore(xmin,xmax,ymax-4,ymax);
  538.          fg_stall(delay);
  539.          fg_restore(xmin,xmin+7,ymin,ymax);
  540.          fg_stall(delay);
  541.  
  542.          xmin += 32;
  543.          xmax -= 32;
  544.          ymin += 20;
  545.          ymax -= 20;
  546.       }
  547.    }
  548. }
  549.  
  550. /****************************************************************************\
  551. *                                                                            *
  552. *  spiral_normal                                                             *
  553. *                                                                            *
  554. *  This is a spiral effect in which we reveal the screen as a series of      *
  555. *  rectangles, emanating from the screen edges and proceeding clockwise to   *
  556. *  the center of the screen.                                                 *
  557. *                                                                            *
  558. \****************************************************************************/
  559.  
  560. void spiral_normal(delay)
  561. int delay;
  562. {
  563.    int xmin, xmax;
  564.    int ymin, ymax;
  565.  
  566.    xmin = 0;
  567.    xmax = 319;
  568.    ymin = 0;
  569.    ymax = 199;
  570.  
  571.    while (xmin < xmax)
  572.    {
  573.       fg_restore(xmin,xmax,ymin,ymin+19);
  574.       fg_stall(delay);
  575.       fg_restore(xmax-31,xmax,ymin,ymax);
  576.       fg_stall(delay);
  577.       fg_restore(xmin,xmax,ymax-19,ymax);
  578.       fg_stall(delay);
  579.       fg_restore(xmin,xmin+31,ymin,ymax);
  580.       fg_stall(delay);
  581.  
  582.       xmin += 32;
  583.       xmax -= 32;
  584.       ymin += 20;
  585.       ymax -= 20;
  586.    }
  587. }
  588.  
  589. /****************************************************************************\
  590. *                                                                            *
  591. *  split_screen                                                              *
  592. *                                                                            *
  593. *  Reveal the top half of from left to right while revealing the bottom half *
  594. *  from right to left.                                                       *
  595. *                                                                            *
  596. \****************************************************************************/
  597.  
  598. void split_screen(delay)
  599. int delay;
  600. {
  601.    register int xmin, xmax;
  602.  
  603.    xmin = 0;
  604.    xmax = 319;
  605.  
  606.    while (xmax > 0)
  607.    {
  608.       fg_restore(xmin,xmin+7,0,99);
  609.       fg_restore(xmax-7,xmax,100,199);
  610.       fg_stall(delay);
  611.       xmin += 8;
  612.       xmax -= 8;
  613.    }
  614. }
  615.  
  616. /****************************************************************************\
  617. *                                                                            *
  618. *  unveil                                                                    *
  619. *                                                                            *
  620. *  Starting at the center, reveal the screen in small horizontal increments  *
  621. *  until we reach the left and right edges.                                  *
  622. *                                                                            *
  623. \****************************************************************************/
  624.  
  625. void unveil(delay)
  626. int delay;
  627. {
  628.    register int xmin, xmax;
  629.  
  630.    xmin = 152;
  631.    xmax = 167;
  632.  
  633.    while (xmin >= 0)
  634.    {
  635.       fg_restore(xmin,xmin+7,0,199);
  636.       fg_restore(xmax-7,xmax,0,199);
  637.       fg_stall(delay);
  638.       xmin -= 8;
  639.       xmax += 8;
  640.    }
  641. }
  642.  
  643. /****************************************************************************\
  644. *                                                                            *
  645. *  venetian_blind                                                            *
  646. *                                                                            *
  647. *  Reveal the screen in four iterations, each revealing every fourth row.    *
  648. *  The effect produced resembles opening a Venetian blind.                   *
  649. *                                                                            *
  650. \****************************************************************************/
  651.  
  652. void venetian_blind(delay)
  653. int delay;
  654. {
  655.    register int y;
  656.  
  657.    for (y = 0; y < 200; y += 4)
  658.       fg_restore(0,319,y,y);
  659.    fg_stall(delay);
  660.  
  661.    for (y = 1; y < 200; y += 4)
  662.       fg_restore(0,319,y,y);
  663.    fg_stall(delay);
  664.  
  665.    for (y = 2; y < 200; y += 4)
  666.       fg_restore(0,319,y,y);
  667.    fg_stall(delay);
  668.  
  669.    for (y = 3; y < 200; y += 4)
  670.       fg_restore(0,319,y,y);
  671. }
  672.